package com.example.chat.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.chat.beans.Admin;
import com.example.chat.beans.Role;
import com.example.chat.beans.Token;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.RoleMapper;
import com.example.chat.mapper.TokenMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class CheckUtils {

    @Resource
    TokenMapper tokenMapper;
    @Resource
    AdminMapper adminMapper;
    @Resource
    RoleMapper roleMapper;

    public ApiResult<Base> checkPassword(String password) {
        String reg = "^[A-Za-z0-9]{6,16}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(password);
        if (matcher.find())
            return null;
        else {
            ApiResult<Base> apiResult = new ApiResult<>();
            apiResult.setStatus(0);
            apiResult.setMsg("请输入6-16位数字或字母的密码");
            return apiResult;
        }
    }

    public ApiResult<Base> isMobile(String mobile) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        if (m.matches())
            return null;
        else {
            ApiResult<Base> apiResult = new ApiResult<>();
            apiResult.setMsg("请输入正确的手机号");
            apiResult.setStatus(0);
            return apiResult;
        }
    }

    public ApiResult<Base> checkEmpty(List<Object> list) {
        ApiResult<Base> apiResult = new ApiResult<>();
        int status = 1;
        for (Object o : list) {
            if (o == null || o.equals("")) {
                status = 0;
                apiResult.setMsg("缺少参数");
                break;
            }
        }
        apiResult.setStatus(status);
        return apiResult;
    }

    public Token checkToken(String token, int userId) {
        QueryWrapper<Token> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id", userId)
                .eq("token", token)
                .gt("last_time", System.currentTimeMillis() - 5 * 24 * 60 * 60 * 1000);
        Token t = tokenMapper.selectOne(wrapper);
        if (t != null) {
            t.setLastTime(System.currentTimeMillis());
            tokenMapper.updateById(t);
        }
        return t;
    }

    public ApiResult<Base> checkToken(List<Object> list, Integer id, String token, String permission) {
        ApiResult<Base> apiResult = new ApiResult<>();
        int status = 0;
        String msg = "";
        if (id == 0 && token.equals("")) {
            status = 404;
            msg = "登录失效";
            apiResult.setStatus(status);
            apiResult.setMsg(msg);
            return apiResult;
        }
        apiResult = checkEmpty(list);
        if (apiResult.getStatus() != 1)
            return apiResult;
        Token t = checkToken(token, id);
        if (t == null) {
            status = 404;
            msg = "登录失效";
        } else {
            if (!permission.equals("")) {
                List<Role> roleList = roleMapper.selectByAdminId(id);
                if (roleList.size() != 0) {
                    for (Role role : roleList) {
                        List<String> permissions = Arrays.asList(role.getPermission().split(","));
                        if (permissions.contains("0") || permissions.contains(permission)) {
                            status = 1;
                            break;
                        } else {
                            msg = "无权限";
                        }
                    }
                } else
                    msg = "无权限";
            } else
                status = 1;
        }
        apiResult.setStatus(status);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public JSONObject getId(Integer id) {
        JSONObject jsonObject = new JSONObject();
        Admin admin = adminMapper.selectOneByAdminId(id);
        jsonObject.put("permission", admin.getPermission());
        return jsonObject;
    }

}
